package servlets.name;

import databean.RequestData;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import servlets.common.ButtonMethod;
import servlets.common.HelperBase;

public class ControllerHelper extends HelperBase {

    private RequestData data =
            new RequestData();

    public ControllerHelper(HttpServletRequest request,
            HttpServletResponse response) {
        super(request, response);
    }

    public Object getData() {
        return data;
    }

    @Override
    public void copyFromSession(Object sessionHelper) {
        if (sessionHelper.getClass() == this.getClass()) {
            data = ((ControllerHelper) sessionHelper).data;
        }
    }

    protected String jspLocation(String page) {
        return "/WEB-INF/" + page;
    }

    @ButtonMethod(buttonName = "editButton", isDefault = true)
    public String editMethod() {
        return jspLocation("Edit.jsp");
    }

    @ButtonMethod(buttonName = "confirmButton")
    public String confirmMethod() {
        fillBeanFromRequest(data);
        return jspLocation("Confirm.jsp");
    }

    @ButtonMethod(buttonName = "processButton")
    public String processMethod() {
        return jspLocation("Process.jsp");
    }

    @Override
    protected void doGet()
            throws ServletException, IOException {
        addHelperToSession("helper", SessionData.READ);

        String address = executeButtonMethod();

        request.getRequestDispatcher(address).forward(request, response);
    }
}
